/**
[f(x)， g(x)， h(x)] 的 复合函数 为 fn(x) = f(g(h(x))) 
 */
// var compose = function (functions) {
//   return function (x) {
//     let res = x;
//     for (let i = functions.length - 1; i >= 0; i--) {
//       res = functions[i](res);
//     }
//     return res;
//   };
// };

var compose = function (functions) {
  return function (x) {
    return functions.reduceRight((pre, curr) => curr(pre), x);
  };
};
const double = x => x * 2;
const addOne = x => x + 1;
const square = x => x * x; //5*5=25 25+1=26 26*2=52
const calculate0 = double(addOne(square(5)))
const calculate = compose([double,addOne,square]);
console.log(calculate(5), calculate0) //52